﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using MyPlanSilverlight.Models;
using MyPlanSilverlight.Utils;
using System.Json;
using System.IO;
using System.Text;
using System.Runtime.Serialization.Json;

namespace MyPlanSilverlight
{
    public partial class RegistPage : Page
    {
        public RegistPage()
        {
            InitializeComponent();
        }

        private static Cache systemCache = Cache.Instance;

        // 当用户导航到此页面时执行。
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void submit_Click(object sender, RoutedEventArgs e)
        {
            if (!validateForm()) return;

            string name = userName.Text.Trim();
            string email = userEmail.Text.Trim();
            string password = userPassword.Text.Trim();
            string confirmPass = confirmPassword.Text.Trim();

            string url = App.Current.Host.Source.ToString();
            url = url.Substring(0, url.IndexOf('/', 7) + 1) + "api/user";
            Uri URL = new Uri(url);

            HttpHelper helper = new HttpHelper(URL, "POST",
                new KeyValuePair<string, string>("Name", name),
                new KeyValuePair<string, string>("Account", email),
                new KeyValuePair<string, string>("Password", password));
            helper.ResponseComplete += new HttpResponseCompleteEventHandler(this.CommandComplete);
            helper.Execute();  
        }

        private bool validateForm()
        {
            string name = userName.Text.Trim();
            string email = userEmail.Text.Trim();
            string password = userPassword.Text.Trim();
            string confirmPass = confirmPassword.Text.Trim();
            if (string.IsNullOrWhiteSpace(name))
            {
                MessageBox.Show("姓名不能为空", "系统提示", MessageBoxButton.OK);
                return false;
            }

            if (string.IsNullOrWhiteSpace(email))
            {
                MessageBox.Show("邮箱不能为空", "系统提示", MessageBoxButton.OK);
                return false;
            }

            if (string.IsNullOrWhiteSpace(password))
            {
                MessageBox.Show("密码不能为空", "系统提示", MessageBoxButton.OK);
                return false;
            }

            if (string.IsNullOrWhiteSpace(confirmPass))
            {
                MessageBox.Show("确认密码不能为空", "系统提示", MessageBoxButton.OK);
                return false;
            }

            if(password != confirmPass)
            {
                MessageBox.Show("两次密码输入不一致", "系统提示", MessageBoxButton.OK);
                return false;

            }

            return true;
        
        }

        private void CommandComplete(HttpResponseCompleteEventArgs e)
        {
            
           

            
            
            //Team team = js.Deserialize<Team>(json);

            this.Dispatcher.BeginInvoke(() => {
                MessageBox.Show(e.Response);

                /*Stream responseStream = new MemoryStream(Encoding.Unicode.GetBytes(e.Response));

                DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(User));
                User user = (User)serializer.ReadObject(responseStream);
                */

                User user = JsonUtils.toEntity<User>(e.Response);

                systemCache["Id"] = user.Id;
                systemCache["Name"] = user.Name;
                systemCache["Account"] = user.Account;
            });
            
        }

        private void GoBack_Click(object sender, RoutedEventArgs e)
        {
            this.Content = new MainPage();
        }  

    }
}
